package com.lwj.test.uuid;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;

public class AppTools {
	public static Object UTIL_OJHID = new Object();
	public static int UTIL_IJHIDCOUNT = 1000;

	public static String getMD5String(String text) {

		StringBuilder md5Str = new StringBuilder();
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(text.getBytes());
			byte[] md5Buf = md5.digest();
			for (int i = 0; i < md5Buf.length; i++) {
				md5Str.append(Integer.toHexString((int) md5Buf[i] & 0xFF));
			}
		} catch (Exception e) {
		}
		return md5Str.toString();
	}

	/**
	 * 生成 18 位的编号
	 * 
	 * @return String
	 */
	public static String getNewID() {
		synchronized (UTIL_OJHID) {
			if (UTIL_IJHIDCOUNT >= 9999) {
				UTIL_IJHIDCOUNT = 1000;
			}
			StringBuffer sb = new StringBuffer(20);
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");

			sb.append(format.format(new java.util.Date()));
			sb.append(UTIL_IJHIDCOUNT++);
			return sb.toString();
		}
	}
	public static String getNewID2() {
		synchronized (UTIL_OJHID) {
			//if (UTIL_IJHIDCOUNT >= 9999) {
			//	UTIL_IJHIDCOUNT = 1000;
			//}
			StringBuffer sb = new StringBuffer(20);
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");

			sb.append(format.format(new java.util.Date()));
			//sb.append(UTIL_IJHIDCOUNT++);
			return sb.toString();
		}
	}
	public static void main(String[] args){
		long startDate = System.currentTimeMillis();
	    for (int i = 0; i < 10; ++i) {
	      System.out.println(getNewID2());
	      //System.out.println(getUUID());
	    }
	    long endDate = System.currentTimeMillis();
	    System.out.println("----"+(endDate-startDate)/1000);
	}
}
